import { getMapInfo } from "@/utils";

async function getFromFastAPI(adcode, year) {
  try {
    const response = await fetch(`http://192.168.80.190:8000/adcode/${adcode}/year/${year}`);
    return response.json();
  } catch (error) {
    console.error('Error:', error);
    throw error; // re-throw the error so it can be caught by the caller
  }
}

export async function selectCityData({ abcode, year } = params) {
  try {
    const data = await getMapInfo(abcode);
    const mapData = await Promise.all(data.map(async (item) => {
      const info = await getFromFastAPI(item.adcode, year);
      return {
        name: item.name,
        value: info, // use the info from the second request as the value
        adcode: item.adcode,
      };
    }));

    return {
      code: 200,
      data: mapData,
    };
  } catch (error) {
    console.error('Error:', error);
    throw error; // re-throw the error so it can be caught by the caller
  }
}

